package com.jiang.daily.Q2353;

import java.util.HashMap;
import java.util.TreeSet;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/02/28 14:51
 */
class Pair1 {
    String cuisine;
    int rating;

    public Pair1(String cuisine, int rating) {
        this.cuisine = cuisine;
        this.rating = rating;
    }
}

class Pair2 implements Comparable<Pair2> {
    int rating;
    String food;

    public Pair2(int rating, String food) {
        this.rating = rating;
        this.food = food;
    }

    // 实现 Comparable 接口，用于 TreeSet 排序
    @Override
    public int compareTo(Pair2 other) {
        if (this.rating != other.rating) {
            return other.rating - this.rating;
        }
        return this.food.compareTo(other.food);
    }
}

class FoodRatings {
    // 显然哈希~
    HashMap<String, Pair1> map1 = new HashMap<String, Pair1>();// 食物名字-烹饪方式+评分(一对一)
    HashMap<String, TreeSet<Pair2>> map2 = new HashMap<String, TreeSet<Pair2>>();// 烹饪方式-评分+名字(有多个！)

    public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
        for (int i = 0; i < foods.length; i++) {
            map1.put(foods[i], new Pair1(cuisines[i], ratings[i]));
            if (map2.containsKey(cuisines[i])) {
                map2.get(cuisines[i]).add(new Pair2(ratings[i], foods[i]));
            } else {
                TreeSet<Pair2> set = new TreeSet<Pair2>();
                set.add(new Pair2(ratings[i], foods[i]));
                map2.put(cuisines[i], set);
            }
        }
    }

    public void changeRating(String food, int newRating) {
        Pair1 pair1 = map1.get(food);
        String cuisine = pair1.cuisine;
        int oldRating = pair1.rating;
        map2.get(cuisine).remove(new Pair2(oldRating, food));
        pair1.rating = newRating;
        map2.get(cuisine).add(new Pair2(newRating, food));
    }

    public String highestRated(String cuisine) {
        return map2.get(cuisine).first().food;
    }
}
